package com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.catalogue;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.google.gson.Gson;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.bean.RiskCourseBean;
import com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.bean.RiskRemarksBean;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoParams;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by callxkj on 2018/1/14.
 * 健康风险管理师  目录adapter
 */

public class HealthRiskCatalogueAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_LEVEL_PARENT = 0;
    public static final int TYPE_LEVEL_ITEM1 = 1;
    public static final int TYPE_LEVEL_ITEM2 = 2;
    public static final int TYPE_LEVEL_ITEM_VIDEO = 3;
    private Gson mGson;
    private CourseItemClickListener mCourseItemClickListener;
    private OnCourseLastSelectListener mCourseLastSelectListener;
    private boolean mIsFirst;
    private List<MultiItemEntity> mBeanList = new ArrayList<>();

    public HealthRiskCatalogueAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_PARENT, R.layout.item_rcv_healthriskcatalogue_parent);
        addItemType(TYPE_LEVEL_ITEM1, R.layout.item_rcv_healthriskcatalogue_child1);
        addItemType(TYPE_LEVEL_ITEM2, R.layout.item_rcv_healthriskcatalogue_child2);
        addItemType(TYPE_LEVEL_ITEM_VIDEO, R.layout.item_rcv_healthriskcatalogue_video);
    }

    public void setDatas(List<MultiItemEntity> beanList, boolean isFirst) {
        mGson = new Gson();
        this.mIsFirst = isFirst;
        mBeanList.clear();
        mBeanList.addAll(beanList);
        if(mIsFirst){
            setNewData(mBeanList);
        }else{
            replaceData(mBeanList);
        }
    }


    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {

        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_PARENT:
                parseTypeParent(helper, item);
                break;
            case TYPE_LEVEL_ITEM1:
                parseTyepItem1(helper, item);
                break;

            case TYPE_LEVEL_ITEM2:
                parseTyepItem2(helper, item);
                break;
            case TYPE_LEVEL_ITEM_VIDEO:
                parseTyepItemVideo(helper, item);
                break;
        }
    }

    /**
     * 解析第一级
     */
    private void parseTypeParent(final BaseViewHolder helper, MultiItemEntity item) {
        final CatalogueParentItem fitem = (CatalogueParentItem) item;
        helper.setText(R.id.by_healthriskcatalogue_parent_tv, fitem.getName());
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = helper.getAdapterPosition();
                if (fitem.isExpanded()) {
                    collapse(pos);
                } else {
                    expand(pos);
                }
            }
        });

        if (fitem.isExpanded()) {
            helper.setImageResource(R.id.by_healthriskcatalogue_parent_iv, R.drawable.by_icon_general_ic_fold);
        } else {
            helper.setImageResource(R.id.by_healthriskcatalogue_parent_iv, R.drawable.by_icon_general_ic_unfold);
        }
    }

    /**
     * 解析第二级
     */
    private void parseTyepItem1(BaseViewHolder helper, MultiItemEntity item) {
        CatalogueChildItem1 item1 = (CatalogueChildItem1) item;
        helper.setText(R.id.by_healthriskcatalogue_item1_tv, item1.getName());
    }


    /**
     * 解析第三级
     */
    private void parseTyepItem2(BaseViewHolder helper, MultiItemEntity item) {
        CatalogueChildItem2 item2 = (CatalogueChildItem2) item;
        helper.setText(R.id.by_healthriskcatalogue_item2_tv, item2.getName());
    }


    /**
     * 解析课件级
     */
    private void parseTyepItemVideo(final BaseViewHolder helper, MultiItemEntity item) {
        final RelativeLayout rootView = helper.getView(R.id.item_healthriskcatalogue_root);
        final CatalogueChildItemVideo videoItem = (CatalogueChildItemVideo) item;
        final RiskCourseBean bean = videoItem.getRiskCourseBean();
        final RiskCourseBean.CurnodeBean curnode = bean.getCurnode();
        String dispname = curnode.getDispname();
        final TextView tvTitle = helper.getView(R.id.item_healthriskcatalogue_tv2);
        TextView tv1 = helper.getView(R.id.item_healthriskcatalogue_tv1);
        TextView tv3 = helper.getView(R.id.item_healthriskcatalogue_tv3);
        tvTitle.setText(dispname == null ? "" : dispname);

        int watchstatus = bean.getWatchstatus();
        if (watchstatus == -1) { //-1未开始学习课程
            tv1.setBackgroundResource(R.drawable.by_bg_healthrisk_video_black);
            tv1.setTextColor(Color.parseColor("#323233"));
            tvTitle.setTextColor(Color.parseColor("#333333"));
        } else {
            RiskCourseBean.Studyrecord studyrecord = bean.getStudyrecord();
            int effecttime = studyrecord.getEffecttime();
            if (effecttime > 0) {  //视频已经通过
                tv1.setBackgroundResource(R.drawable.by_bg_healthrisk_video_gray);
                tv1.setTextColor(Color.parseColor("#CCCCCC"));
                tvTitle.setTextColor(Color.parseColor("#CCCCCC"));
            } else {
                tv1.setBackgroundResource(R.drawable.by_bg_healthrisk_video_black);
                tv1.setTextColor(Color.parseColor("#323233"));
                tvTitle.setTextColor(Color.parseColor("#333333"));
            }
        }


        //--判断当前课程是不是上次播放的课程 是的话 就显示绿色
        String cwid = curnode.getCwid();
        String lastlearnCwid = videoItem.getLastlearnCwid();
        if (cwid.equals(lastlearnCwid)) {
            tv1.setBackgroundResource(R.drawable.by_bg_healthrisk_video_blue);
            tv1.setTextColor(Color.parseColor("#1BA6FF"));
            tvTitle.setTextColor(Color.parseColor("#1BA6FF"));
            if (!mIsFirst) {
                if (mCourseLastSelectListener != null) {
                   mCourseLastSelectListener.onSelect(helper.getAdapterPosition());
                }
            }
        }


        //判断时候有课后练习题 并且判断练习题是否是学习过
        String remarks = curnode.getRemarks();
        RiskRemarksBean riskRemarksBean = mGson.fromJson(remarks, RiskRemarksBean.class);
        if (riskRemarksBean != null && riskRemarksBean.isHasqa()) { //是否有题
            tv3.setVisibility(View.VISIBLE);
            if (riskRemarksBean.isQapass()) { //课后练习题已经通过
                tv3.setTextColor(Color.parseColor("#DAE1E6"));
                tv3.getPaint().setAntiAlias(true);
                tv3.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            } else {
                tv3.setTextColor(Color.parseColor("#7A8D99"));
                tv3.getPaint().setAntiAlias(true);
                tv3.getPaint().setFlags(0);
            }
        } else {  //没有课后练习题
            tv3.setVisibility(View.GONE);
        }


        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCourseItemClickListener != null) {
                    VideoParams videoParams = new VideoParams();
                    videoParams.mCourseId = videoItem.getCourseid();
                    videoParams.mStudentNo = videoItem.getStudentno();
                    videoParams.mCwid = curnode.getCwid();
                    videoParams.mPathNo = curnode.getPathno();
                    videoParams.mCwidName = curnode.getDispname();
                    mCourseItemClickListener.onClick(videoParams);
                }
            }
        });
    }

    /**
     * 课程item点击
     */
    public void setOnCourseItemClickListener(CourseItemClickListener courseItemClickListener) {
        this.mCourseItemClickListener = courseItemClickListener;
    }

    public interface CourseItemClickListener {
        void onClick(VideoParams videoParams);
    }

    /**
     * 课程item点击
     */
    public void setOnCourseLastSelectListener(OnCourseLastSelectListener onCourseLastSelectListener) {
        this.mCourseLastSelectListener = onCourseLastSelectListener;
    }

    public interface OnCourseLastSelectListener {
        void onSelect(int position);
    }

}
